package com.ihcy.base.core.enums;

import javax.persistence.AttributeConverter;

/**
 * 用于 jpa 值 转换
 * @param <T>
 */
public abstract class EnumAbilityJpaConvert<T> implements AttributeConverter<EnumAbility<T>, T> {

    private final Class<? extends Enum> enumClass;

    protected EnumAbilityJpaConvert(Class<? extends Enum> enumClass) {
        this.enumClass = enumClass;
    }

    @Override
    public T convertToDatabaseColumn(EnumAbility<T> anEnum) {
        return anEnum.getCode();
    }

    @Override
    public EnumAbility<T> convertToEntityAttribute(T code) {
        return EnumUtil.getEnumByCode(enumClass, code);
    }
}
